package restaurant.iterators;

import java.util.ArrayList;
import java.util.NoSuchElementException;

import restaurant.MenuItem;

/**
 * An iterator to view the menu by preparation times.
 * 
 * @author Odessa
 */

public class ByPrepTimeIterator implements Iterator
{

	/** The preparation time to view the menu by. */
	private int prepTime;

	/** The iterator through the menu. */
	private java.util.Iterator<MenuItem> menu_iterator;

	/** The next item in the menu. */
	private MenuItem nextItem;



	/**
	 * Instantiates a new by prep time iterator.
	 * 
	 * @param menu the menu of items.
	 * @param prepTime the preparation time by which to view the menu.
	 */
	public ByPrepTimeIterator(ArrayList<MenuItem> menu, int prepTime) {
		this.prepTime = prepTime;
		menu_iterator = menu.iterator();
	}



	/**
	 * Determines if there is another item on the menu with a lower preparation
	 * time than the one specified.
	 * 
	 * @return {@code true} if there are more items on the menu which have a
	 *         lower preparation time than the one specified.
	 * @see restaurant.iterators.Iterator#hasNext()
	 */
	@Override
	public boolean hasNext()
	{
		boolean matchFound = false;

		while (menu_iterator.hasNext()) {
			MenuItem temp = (MenuItem) menu_iterator.next();

			if (temp.getCurrentPrepTime() <= prepTime) {
				matchFound = true;
				nextItem = temp;
				break;
			}
		}

		if (matchFound == true) {
		}
		else
			nextItem = null;

		return matchFound;
	}



	/**
	 * Returns the next item on the menu which has lower preparation time than
	 * the one specified.
	 * 
	 * @return the next item on the menu which has a lower preparation time than
	 *         the one specified.
	 * @throws NoSuchElementException if the menu has no more items with a lower
	 *             preparation time.
	 * @see restaurant.iterators.Iterator#next()
	 */
	@Override
	public Object next() throws NoSuchElementException
	{
		if (nextItem == null) {
			throw new NoSuchElementException(
					"There are no more items on the menu with a lower preparation time.");
		}
		else
			return nextItem;
	}

}
